/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import datamodel.StorageInformer;
import view.commands.BackCommand;
import view.commands.EmptyCommand;
import java.util.TimeZone;
import javax.microedition.media.Manager;

/**
 *
 * @author Kvest
 */
public class InfoForm extends BasicForm
{
    private TextArea info;
    
    public InfoForm()
    {
        super("info");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
        
        info = new TextArea(getInfo(), 1, 30);
        info.setEditable(false);
        info.setGrowByContent(true); 
        addComponent(info);
        
        addCommand(new EmptyCommand());
        addCommand(new BackCommand());
    }
    
    public void show()
    {
        //update info
        info.setText(getInfo());
        
        super.show();
    }
    
    private String getInfo()
    {
        String result = "IMAP mobile " + IMAPMIDlet.getMidlet().getAppProperty("MIDlet-Version") + "\n\n";
        
        //Rms-заказы
        StorageInformer info = IMAPMIDlet.getMidlet().getOrderManager().getStorageMemoryInfo();
        result += (new Label("rms_orders")).getText() + " " + info.getUsedMemorySize() + "\\" + info.getFreeMemorySize() + "\n\n";
        
        //Rms-сообщения
        info = IMAPMIDlet.getMidlet().getMessageManager().getStorageMemoryInfo();
        result += (new Label("rms_messages")).getText() + " " + info.getUsedMemorySize() + "\\" + info.getFreeMemorySize() + "\n\n";
        
        //Rms-стоянки
        info = IMAPMIDlet.getMidlet().getPositionManager().getStorageMemoryInfo();
        result += (new Label("rms_positions")).getText() + " " + info.getUsedMemorySize() + "\\" + info.getFreeMemorySize() + "\n\n";
        
        //microedition.platform
        result += "microedition.platform: " + (System.getProperty("microedition.platform") != null ? System.getProperty("microedition.platform") : "[NULL]") + "\n\n"; 
        
        //device.model
        result += "device.model: " + (System.getProperty("device.model") != null ? System.getProperty("device.model") : "[NULL]") + "\n\n";
        
        //funlights.product
        result += "funlights.product: " + (System.getProperty("funlights.product") != null ? System.getProperty("funlights.product") : "[NULL]") + "\n\n";
        
        //Версия FileConnection
        result += (new Label("version")).getText() +  " FileConnection: " + 
                (System.getProperty("microedition.io.file.FileConnection.version") != null ? System.getProperty("microedition.io.file.FileConnection.version") : "[NULL]") + "\n\n";
        
        //MediaContents
        result += "MediaContents: " + getMediaContents() + "\n\n";
    
        //TimeZones
        result += "TimeZones: " + getTimeZones() + "\n\n";
    
        result += "Free memory: " + Runtime.getRuntime().freeMemory() + " of " + Runtime.getRuntime().totalMemory() + "\n\n";  
        
        return result;
    }
    
    private String getMediaContents()
    {
        String str = "";
        try
        {
          String[] contents = Manager.getSupportedContentTypes(null);
          for (int i = 0; i < contents.length; i++)
            str = str + contents[i] + "\n";
        }catch (Exception ex){}
        
        return str;
    }
    
    private String getTimeZones()
    {
        String str = "";
        String[] timezones = TimeZone.getAvailableIDs();
        for (int i = 0; i < timezones.length; i++)
        {
            str = str + timezones[i] + "\n";
        }
    
        return str;
    }
}
